package com.ruoyi.web.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.List;


public abstract class BaseController<E> extends com.ruoyi.common.core.controller.BaseController {
    @Autowired private ISysDeptService deptService;
    abstract protected IService<E> getService();
    private SysDept dept;

    public QueryWrapper getQueryWrap(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("dept_id",getLoginUser().getDeptId());
        return queryWrapper;
    }

    public TableDataInfo listByDept(Integer pageNum, Integer pageSize, QueryWrapper<E> queryWrapper) {
        queryWrapper.orderByDesc("id");
        Page<E> list = getService().page(new Page<>(pageNum,pageSize),queryWrapper);
        return getDataTable(list);
    }


    /**
     * 没有ID主键情况下
     * @param pageNum
     * @param pageSize
     * @param e
     * @return
     */
    public TableDataInfo listByUncode(Integer pageNum, Integer pageSize, E e) {
        QueryWrapper wrapper=new QueryWrapper<E>();
        wrapper.setEntity(e);
        Page<E> list = getService().page(new Page<>(pageNum,pageSize),wrapper);
        return getDataTable(list);
    }

    public TableDataInfo list(Integer pageNum, Integer pageSize, E e) {
        QueryWrapper wrapper=new QueryWrapper<E>();
        wrapper.setEntity(e);
        wrapper.orderByDesc("id");
        Page<E> list = getService().page(new Page<>(pageNum,pageSize),wrapper);
        return getDataTable(list);
    }

    public void exportExcel(HttpServletResponse response, E e, String sheetName) {
        QueryWrapper wrapper=new QueryWrapper<E>();
        wrapper.setEntity(e);
        List<E> list = getService().list(wrapper);
        ExcelUtil<E> util = new ExcelUtil<E>((Class<E>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
        util.exportExcel(response, list, sheetName);
    }

    public SysDept getDept(){
        this.dept=deptService.selectDeptById(this.getLoginUser().getDeptId());
        return this.dept;
    }

    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(getService().getById(id));
    }

    public AjaxResult getInfoBy(@PathVariable("unifiedCode") String unifiedCode) {
        return success(getService().getById(unifiedCode));
    }


    public AjaxResult add(@RequestBody E e) {
        return toAjax(getService().save(e));
    }


    public AjaxResult edit(@RequestBody E e) {
        return toAjax(getService().updateById(e));
    }


    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(getService().removeByIds(Arrays.asList(ids)));
    }


    public AjaxResult remove(@PathVariable String[] unifiedCodes) {
        return toAjax(getService().removeByIds(Arrays.asList(unifiedCodes)));
    }

}
